package canglang.monitor.user.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.model.LabelValue;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.user.model.UserModel;
import canglang.monitor.user.service.UserMgr;
import canglang.monitor.user.to.SearchTO;

import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

@SuppressWarnings("unchecked")
public class UserAction extends BaseAction {

    private static final long serialVersionUID = 5621374765031457239L;

    private UserModel userModel;

    private UserMgr userMgr;

    private PaginatedListHelper helper;

    private SearchTO searchTO;
    
    private List viewTypeList;

    public List getViewTypeList() {
		return viewTypeList;
	}

	public void setViewTypeList(List viewTypeList) {
		this.viewTypeList = viewTypeList;
	}

	public UserModel getUserModel() {
        return userModel;
    }

    public void setUserModel(UserModel userModel) {
        this.userModel = userModel;
    }

    public void setUserMgr(UserMgr userMgr) {
        this.userMgr = userMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchTO searchTO) {
        this.searchTO = searchTO;
    }

    @Element(value = UserModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }

    public String searchUser() {
        if (searchTO == null) {
            searchTO = new SearchTO();
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("USER_ID");
        }
        helper = userMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String deleteUser() {
        for (int i = 0; i < helper.getList().size(); i++) {
            UserModel u = (UserModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                userMgr.deleteUser(u.getSelectId());
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return searchUser();
    }

    public String addUser() {
        String userId = userModel.getUserId();
        UserModel u = userMgr.getUser(userId);
        if (u != null) {
            this.addActionError(AppDefaultProperty.getProperty("label.record.already.exist"));
            return INPUT;
        }
        UserModel cUser = (UserModel) this.getSession().getAttribute("user");
        userModel.setCreateId(cUser.getUserId());
        userModel.setUpdateId(cUser.getUserId());
        Date date = DateUtil.getDate();
        userModel.setCreateDt(date);
        userModel.setUpdateDt(date);
        if (userModel.getUserRole().equalsIgnoreCase("A")) {
            userModel.setDeptId(0);
        }
        userModel = userMgr.addUser(userModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
    	setViewTypeValues();
        return SUCCESS;
    }
    
    public String viewAdd() {
    	setViewTypeValues();
        return SUCCESS;
    }

    public String viewUser() {
    	setViewTypeValues();
        String selectId = userModel.getSelectId();
        userModel = userMgr.getUser(selectId);
        return SUCCESS;
    }

    public String saveUser() {
        UserModel cUser = (UserModel) this.getSession().getAttribute("user");
        userModel.setUpdateId(cUser.getUserId());
        userModel.setUpdateDt(DateUtil.getDate());
        if (userModel.getUserRole().equalsIgnoreCase("A")) {
            userModel.setDeptId(0);
        }
        userMgr.saveUser(userModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        setViewTypeValues();
        return SUCCESS;
    }
    
    private void setViewTypeValues(){
    	viewTypeList=new ArrayList();
        LabelValue b1=new LabelValue();
        LabelValue b2=new LabelValue();
        b1.setLabel(AppDefaultProperty.getProperty("label.title.view.permission.all"));
        b1.setValue("Y");
        b2.setLabel(AppDefaultProperty.getProperty("label.title.view.permission.dept"));
        b2.setValue("N");
        viewTypeList.add(b1);
        viewTypeList.add(b2);
    }

}
